package com.woniu.fly.fairmall.db.service;

import com.woniu.fly.fairmall.db.dao.QuestAnswerDao;
import com.woniu.fly.fairmall.db.entity.QuestAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 问卷答案表(QuestAnswer)表服务实现类
 *
 * @author makejava
 * @since 2020-03-19 14:23:09
 */
@Service("questAnswerService")
public class QuestAnswerService {
    @Resource
    private QuestAnswerDao questAnswerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */

    public QuestAnswer queryById(Long id) {
        return this.questAnswerDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */

    public List<QuestAnswer> queryAllByLimit(int offset, int limit) {
        return this.questAnswerDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param questAnswer 实例对象
     * @return 实例对象
     */

    public QuestAnswer insert(QuestAnswer questAnswer) {
        questAnswer.setAddTime(LocalDateTime.now());
        // 查询排序
        Integer sort = questAnswerDao.queryMaxSort(questAnswer);
        questAnswer.setSort(sort == null ? 0 : ++sort);
        this.questAnswerDao.insert(questAnswer);
        return questAnswer;
    }

    /**
     * 修改数据
     *
     * @param questAnswer 实例对象
     * @return 实例对象
     */

    public QuestAnswer update(QuestAnswer questAnswer) {
        this.questAnswerDao.update(questAnswer);
        return this.queryById(questAnswer.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */

    public boolean deleteById(Long id) {
        return this.questAnswerDao.deleteById(id) > 0;
    }
}
